package io.renren.modules.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.shop.entity.CarEntity;
import io.renren.modules.shop.entity.ProComByCusEntity;
import io.renren.modules.shop.entity.ProductEntity;
import io.renren.modules.shop.service.ProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.shop.entity.ProductCommentEntity;
import io.renren.modules.shop.service.ProductCommentService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author Yangqianyong
 * @email sunlightcs@gmail.com
 * @date 2021-11-17 15:35:51
 */
@RestController
@RequestMapping("shop/productcomment")
public class ProductCommentController {
    @Autowired
    private ProductCommentService productCommentService;
    @Autowired
    private ProductService productService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("shop:productcomment:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productCommentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{commentId}")
//    @RequiresPermissions("shop:productcomment:info")
    public R info(@PathVariable("commentId") Long commentId){
		ProductCommentEntity productComment = productCommentService.getById(commentId);

        return R.ok().put("productComment", productComment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("shop:productcomment:save")
    public R save(@RequestBody ProductCommentEntity productComment){
		productCommentService.save(productComment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("shop:productcomment:update")
    public R update(@RequestBody ProductCommentEntity productComment){
		productCommentService.updateById(productComment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("shop:productcomment:delete")
    public R delete(@RequestBody Long[] commentIds){
		productCommentService.removeByIds(Arrays.asList(commentIds));

        return R.ok();
    }

//    添加商品评论 addComment 接收：	商品id：productId 	用户id customerId
//    评价标题：title	评价内容 ： content
//            返回成功码
//    public R addCom

//    增加接口 查询商品评论  commentList 接收：商品id
//    返回：商品评论表的所有内容和评论用户的名称和头像（多表查询）
    @RequestMapping("/commentList")
    public R commentList(@RequestParam Long id){
        List<ProComByCusEntity> list = productCommentService.commentList(id);
        ProductEntity productEntity = productService.getById(id);
        String productName = productEntity.getProductName();
        return Objects.requireNonNull(R.ok().put("productName", productName)).put("list",list);
    }
}
